package com.vq.controller;

import com.vq.common.R;
import com.vq.entity.Critique;
import com.vq.entity.User;
import com.vq.service.CritiqueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <h2>动态评论表业务层</h2>
 */
@RestController
@RequestMapping("/Critique")
public class CritiqueController {
    @Autowired
    HttpSession session;

    @Autowired
    CritiqueService critiqueService;

    @PostMapping
    public R insert(@RequestBody Critique critique) {
        R r = new R(0);
        User user = (User) session.getAttribute("myUser");
        if (user==null) return r;
        critique.setUsername(user.getUsername());
        critique.setAccount(user.getAccount());
        if(critiqueService.insert(critique)) {
            r.setCode(1);
        }
        return r;
    }

    @GetMapping("/{trendsId}")
    public R selectByTrendsId(@PathVariable Integer trendsId) {
        List<Critique> critiques = critiqueService.selectByTrendsId(trendsId.longValue());
        return new R(1, critiques);
    }
}
